package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.db.AppConfig;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayConfig;
import com.gzhryc.shared_device.common.payment.dao.enums.EAppType;
import com.gzhryc.shared_device.common.payment.dao.enums.WXPayConfigEnum;
import com.gzhryc.shared_device.common.payment.dao.models.WXTokenHttpConfig;
import com.gzhryc.shared_device.common.payment.services.AppConfigService;
import com.gzhryc.shared_device.common.payment.services.WXPayConfigService;
import com.gzhryc.shared_device.common.payment.services.dto.WXPayConfigSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class WXPayConfigController {

	static Logger log = Logger.getLogger(WXPayConfigController.class);

	@Route(value = { "/config/wxpay/list.html" })
	public String list(HttpServletRequest request) {
		request.setAttribute("appTypeList", WXPayConfigEnum.AppType.values());
		return "/config/wxpay/list.html";
	}

	@Route(value = "/config/wxpay/list.data")
	public ListResult<WXPayConfig> listData(HttpServletRequest request) {
		ListResult<WXPayConfig> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");

		WXPayConfigSearch search = WebUtils.getParameter(request, WXPayConfigSearch.class);
		Integer pageNo = WebUtils.getInteger(request, "page", 1);
		Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

		result.getBody().setTotal(WXPayConfigService.self().countJoinBySearch(search));
		result.getBody().setData(WXPayConfigService.self().findJoinBySearch(search, pageNo, pageLimit));

		if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
			result.setCode(DtoConstants.SUCCESS, null);
		}

		return result;
	}

	@Route(value = { "/config/wxpay/add.html" })
	public String add(HttpServletRequest request) {
		request.setAttribute("appTypeList", WXPayConfigEnum.AppType.values());
		request.setAttribute("tokenTypeList",WXPayConfigEnum.TokenType.values());
		request.setAttribute("stateList",WXPayConfigEnum.State.values());
		return "/config/wxpay/edit.html";
	}

	@Route(value = { "/config/wxpay/add.do" })
	public AjaxResult doAdd(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败");
		WXPayConfig wxpayConfig = WebUtils.getParameter(request, WXPayConfig.class);
		try {
			if(WXPayConfigEnum.TokenType.HttpGet.index().equals(wxpayConfig.getTokenType())){
				WXTokenHttpConfig httpConfig = WebUtils.getParameter(request,"config",WXTokenHttpConfig.class);
				if(StringTools.isBlank(httpConfig.getUrl()) || StringTools.isBlank(httpConfig.getToken())){
					result.setMsg("请选择获取的URL和Token");
					return result;
				}
				wxpayConfig.setTokenConfigJson(JsonTools.toJson(httpConfig));
			}
			if(WXPayConfigService.self().add(wxpayConfig)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}

	@Route(value = { "/config/wxpay/update.html" })
	public String update(HttpServletRequest request) {
		String appId = WebUtils.getString(request, "id");
		if(StringUtils.isNotBlank(appId)) {
			WXPayConfig wxpayConfig = WXPayConfigService.self().getJoinByAppId(appId);
			if(wxpayConfig != null) {
				WebUtils.setAttribute(request, wxpayConfig);
				if(WXPayConfigEnum.TokenType.HttpGet.index().equals(wxpayConfig.getTokenType())
					&& StringTools.isNotBlank(wxpayConfig.getTokenConfigJson())){
					WXTokenHttpConfig httpConfig = JsonTools.fromJson(wxpayConfig.getTokenConfigJson(),WXTokenHttpConfig.class);
					request.setAttribute("config",httpConfig);
				}
			}
		}
		request.setAttribute("appTypeList", WXPayConfigEnum.AppType.values());
		request.setAttribute("tokenTypeList",WXPayConfigEnum.TokenType.values());
		request.setAttribute("stateList",WXPayConfigEnum.State.values());
		return "/config/wxpay/edit.html";
	}

	@Route(value = { "/config/wxpay/update.do" })
	public AjaxResult doUpdate(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败");
		WXPayConfig wxpayConfig = WebUtils.getParameter(request, WXPayConfig.class);
		try {
			if(WXPayConfigEnum.TokenType.HttpGet.index().equals(wxpayConfig.getTokenType())){
				WXTokenHttpConfig httpConfig = WebUtils.getParameter(request,"config",WXTokenHttpConfig.class);
				if(StringTools.isBlank(httpConfig.getUrl()) || StringTools.isBlank(httpConfig.getToken())){
					result.setMsg("请选择获取的URL和Token");
					return result;
				}
				wxpayConfig.setTokenConfigJson(JsonTools.toJson(httpConfig));
			}
			if(WXPayConfigService.self().updateNotNull(wxpayConfig)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}

	@Route(value = { "/config/wxpay/busi_conf/update.html" })
	public String updateBusinessConfig(HttpServletRequest request) {
		String appId = WebUtils.getString(request, "appId");
		if(StringUtils.isNotBlank(appId)) {
			AppConfig appConfig = AppConfigService.self().getByKeys(appId, EAppType.Weixin.index());
			WebUtils.setAttribute(request,appConfig);
		}
		return "/config/wxpay/business.html";
	}

	@Route(value = { "/config/wxpay/busi_conf/save.do" })
	public AjaxResult doBusinessConfigUpdate(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败");
		AppConfig appConfig = WebUtils.getParameter(request, AppConfig.class);
		try {
			appConfig.setType(EAppType.Weixin.index());
			if(AppConfigService.self().save(appConfig)){
				result.setCode(DtoConstants.SUCCESS,null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}

	@Route(value = { "/config/wxpay/del.do" })
	public AjaxResult doDelete(HttpServletRequest request) {
		AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
		String appId = WebUtils.getString(request, "id");
		try {
			if(WXPayConfigService.self().deleteByAppId(appId)) {
				result.setCode(DtoConstants.SUCCESS, null);
			}
		} catch (LogicException e) {
			result.setMsg(e.getMessage());
		}
		return result;
	}
}
